package com.bjx.common.mybatis.plus.config;


import com.bjx.common.security.util.LoginUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**用来自动设置数据库字段的类
 * @author liushuai
 *
 */
@Component
public class MetaObjectHandler implements com.baomidou.mybatisplus.core.handlers.MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
       String userId = LoginUtil.loginInfo().getUserId();
       this.setFieldValByName("createUser",Long.valueOf(userId),metaObject);
       this.setFieldValByName("updateUser",Long.valueOf(userId),metaObject);
       this.setFieldValByName("createDate",new Date(),metaObject);
       this.setFieldValByName("updateDate",new Date(),metaObject);


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String userId = LoginUtil.loginInfo().getUserId();
        this.setFieldValByName("updaterId",Long.valueOf(userId),metaObject);
        this.setFieldValByName("updateDate",new Date(),metaObject);
    }
}
